<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态 1 为上架 0为下架  默认为上架" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态 1 为上架 0为下架  默认为上架" clearable>
          <el-option
            v-for="dict in dict.type.ui_product_selected"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ui:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ui:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ui:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ui:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="proId" />
      <el-table-column label="店铺id" align="center" prop="shopId" />
      <el-table-column label="分类id" align="center" prop="categoryId" />
      <el-table-column label="0:普通商品" align="center" prop="proType" />
      <el-table-column label="产品名称" align="center" prop="proName" />
      <el-table-column label="配送方式 1:商家配送 2：用户自提" align="center" prop="deliveryType" />
      <el-table-column label="原价" align="center" prop="proPrice" />
      <el-table-column label="现价" align="center" prop="proReprice" />
      <el-table-column label="商品介绍" align="center" prop="proTitle" />
      <el-table-column label="状态 1 为上架 0为下架  默认为上架" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ui_product_selected" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="商品库存" align="center" prop="proStock" />
      <el-table-column label="商品图片" align="center" prop="proImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.proImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商品总数" align="center" prop="proNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ui:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ui:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改普通商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="分类id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类id" />
        </el-form-item>
        <el-form-item label="0:普通商品" prop="proType">
          <el-input v-model="form.proType" placeholder="请输入0:普通商品" />
        </el-form-item>
        <el-form-item label="产品名称" prop="proName">
          <el-input v-model="form.proName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="配送方式 1:商家配送 2：用户自提" prop="deliveryType">
          <el-input v-model="form.deliveryType" placeholder="请输入配送方式 1:商家配送 2：用户自提" />
        </el-form-item>
        <el-form-item label="原价" prop="proPrice">
          <el-input v-model="form.proPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="现价" prop="proReprice">
          <el-input v-model="form.proReprice" placeholder="请输入现价" />
        </el-form-item>
        <el-form-item label="商品介绍" prop="proTitle">
          <el-input v-model="form.proTitle" placeholder="请输入商品介绍" />
        </el-form-item>
        <el-form-item label="状态 1 为上架 0为下架  默认为上架" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态 1 为上架 0为下架  默认为上架">
            <el-option
              v-for="dict in dict.type.ui_product_selected"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品库存" prop="proStock">
          <el-input v-model="form.proStock" placeholder="请输入商品库存" />
        </el-form-item>
        <el-form-item label="商品图片">
          <image-upload v-model="form.proImg"/>
        </el-form-item>
        <el-form-item label="商品总数" prop="proNum">
          <el-input v-model="form.proNum" placeholder="请输入商品总数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/ui/product";

export default {
  name: "Product",
  dicts: ['ui_product_selected'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 普通商品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: null,
        state: null,
        proImg: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopId: [
          { required: true, message: "店铺id不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类id不能为空", trigger: "blur" }
        ],
        proType: [
          { required: true, message: "0:普通商品不能为空", trigger: "blur" }
        ],
        proName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        deliveryType: [
          { required: true, message: "配送方式 1:商家配送 2：用户自提不能为空", trigger: "blur" }
        ],
        proPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        proReprice: [
          { required: true, message: "现价不能为空", trigger: "blur" }
        ],
        proTitle: [
          { required: true, message: "商品介绍不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态 1 为上架 0为下架  默认为上架不能为空", trigger: "change" }
        ],
        proStock: [
          { required: true, message: "商品库存不能为空", trigger: "blur" }
        ],
        proImg: [
          { required: true, message: "商品图片不能为空", trigger: "blur" }
        ],
        proNum: [
          { required: true, message: "商品总数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询普通商品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        proId: null,
        shopId: null,
        categoryId: null,
        proType: null,
        proName: null,
        deliveryType: null,
        proPrice: null,
        proReprice: null,
        proTitle: null,
        state: null,
        proStock: null,
        proImg: null,
        proNum: null,
        remark1: null,
        remark2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加普通商品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const proId = row.proId || this.ids
      getProduct(proId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改普通商品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.proId != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const proIds = row.proId || this.ids;
      this.$modal.confirm('是否确认删除普通商品管理编号为"' + proIds + '"的数据项？').then(function() {
        return delProduct(proIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ui/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
